/*
 * net_qrf.c
 *
 *  Created on: 2023年8月24日
 *      Author: mcealt_DS
 */

#include "net_qrf.h"
#include "device.h"
#include "string.h"
#include "app_log.h"
#include "app_file_packet_api.h"
/************** get qrf *****************/
uint8_t getThisQrfR1(void)
{
    uint8_t data = 0;
    getThisParam(ENbyQrfR1, &data);
    return data;
}
uint8_t getThisQrfR2(void)
{
    uint8_t data = 0;
    getThisParam(ENbyQrfR2, &data);
    return data;
}
uint8_t getThisQrfH1(void)
{
    uint8_t data = 0;
    getThisParam(ENbyQrfH1, &data);
    return data;
}
uint8_t getThisQrfH2(void)
{
    uint8_t data = 0;
    getThisParam(ENbyQrfH2, &data);
    return data;
}

uint8_t getDevicQrfR1(uint8_t deviceId)
{
    uint8_t data = 0;
    getDeviceParam(deviceId, ENpbyNodeQrfR1First, &data);
    return data;
}
uint8_t getDevicQrfR2(uint8_t deviceId)
{
    uint8_t data = 0;
    getDeviceParam(deviceId, ENpbyNodeQrfR2First, &data);
    return data;
}
uint8_t getDevicQrfH1(uint8_t deviceId)
{
    uint8_t data = 0;
    getDeviceParam(deviceId, ENpbyNodeQrfH1First, &data);
    return data;
}
uint8_t getDevicQrfH2(uint8_t deviceId)
{
    uint8_t data = 0;
    getDeviceParam(deviceId, ENpbyNodeQrfH2First, &data);
    return data;
}

/************** Set qrf *****************/
int setThisQrfR1(uint8_t *data)
{
    return setThisParam(ENbyQrfR1, data);
}
int setThisQrfR2(uint8_t *data)
{
    return setThisParam(ENbyQrfR2, data);
}
int setThisQrfH1(uint8_t *data)
{
    return setThisParam(ENbyQrfH1, data);
}
int setThisQrfH2(uint8_t *data)
{
    if(!data){
        return 1;
    }
    uint8_t qrfh2 = data[0];
    consumption_deal_qrfh2(qrfh2);
    return setThisParam(ENbyQrfH2, data);
}

int setDevicQrfR1(uint8_t deviceId, uint8_t *data)
{
    return setDeviceParam(deviceId, ENpbyNodeQrfR1First, data);
}
int setDevicQrfR2(uint8_t deviceId, uint8_t *data)
{
    return setDeviceParam(deviceId, ENpbyNodeQrfR2First, data);
}
int setDevicQrfH1(uint8_t deviceId, uint8_t *data)
{
    return setDeviceParam(deviceId, ENpbyNodeQrfH1First, data);
}
int setDevicQrfH2(uint8_t deviceId, uint8_t *data)
{
    return setDeviceParam(deviceId, ENpbyNodeQrfH2First, data);
}
/*********************************************/

// QRF

uint8_t net_qrf_init(void)
{
    uint8_t qrf = 0;
#ifdef RFM_DEVICE
    if (getThisParam(ENbyQrfR1, &qrf) != 0) // 未存
    {
        if (setThisQrfR1(&qrf) != 0) // 存失败
            return false;
    }
    if (getThisParam(ENbyQrfR2, &qrf) != 0) // 未存
    {
        if (setThisQrfR2(&qrf) != 0) // 存失败
            return false;
    }
    if (getThisParam(ENbyQrfH1, &qrf) != 0) // 未存
    {
        if (setThisQrfH1(&qrf) != 0) // 存失败
            return false;
    }
    if (getThisParam(ENbyQrfH2, &qrf) != 0) // 未存
    {
        if (setThisQrfH1(&qrf) != 0) // 存失败
            return false;
    }
#endif
#if 0
#ifdef HUB_DEVICE // 第一次初始化会比较久
    for (size_t rfmid = 1; rfmid < NODE_MAX_COUNT; rfmid++)
    {
        if (getDeviceParam(rfmid, ENpbyNodeQrfR1First, &qrf) != 0) // 未存
        {
            if (setDeviceParam(rfmid, ENpbyNodeQrfR1First, &qrf) != 0) // 存失败
                return false;
        }
        if (getDeviceParam(rfmid, ENpbyNodeQrfR2First, &qrf) != 0) // 未存
        {
            if (setDeviceParam(rfmid, ENpbyNodeQrfR2First, &qrf) != 0) // 存失败
                return false;
        }
        if (getDeviceParam(rfmid, ENpbyNodeQrfH1First, &qrf) != 0) // 未存
        {
            if (setDeviceParam(rfmid, ENpbyNodeQrfH1First, &qrf) != 0) // 存失败
                return false;
        }
        if (getDeviceParam(rfmid, ENpbyNodeQrfH2First, &qrf) != 0) // 未存
        {
            if (setDeviceParam(rfmid, ENpbyNodeQrfH2First, &qrf) != 0) // 存失败
                return false;
        }
    }

#endif
#endif
    return 0;
}

// int getThisParam(DEVICE_ATTRIBUTE_EN attr, void *pData);
// int getDeviceParam(uint8_t nodeId, DEVICE_ATTRIBUTE_EN attr, void *pData);
